//
//  RFPOSUIEngine.m
//  HPShellTest
//
//  Created by marco on 14/12/19.
//  Copyright (c) 2014年 marco. All rights reserved.
//

#import <HandPaySwiperModule/HandPaySwiperModule.h>
#import "RFPOSUIEngine.h"
#import "HPUIEngine.h"
#import "ZZTongUIEngine.h"
#import "Utility.h"



// private define
@interface RFPOSUIEngine()
@property(nonatomic, retain) HPSelectView* selectView;
@end

@implementation RFPOSUIEngine

-(instancetype)initWithPOSProcess:(POSProcess*)workingProcess
{
    self = [super init];
    self.workingProcess = workingProcess;
    self.workingProcess.uiDelegate = self;
    return self;
}

-(void)dealloc
{
    self.workingProcess = nil;
    self.selectView = nil;
    [super dealloc];
}

- (void)notifySwipe:(id)process { [[HPUIEngine getInstance] showScreen:SCR_SWIPER]; }

- (void)notifyInputPin:(id)process appInput:(BOOL)appInput
{
    if (appInput)
    {
        NSDictionary * params = nil;
        NSString* plainAmountStr = self.workingProcess.plainAmount;
        params = plainAmountStr.length == 0 ? @{@"delegate":self} : @{@"delegate":self,@"amount":[Utility getFormatMoneyAndSymbol:plainAmountStr]};
        [[HPUIEngine getInstance] showScreen:SCR_INPUT_PASSWORD params:params];
        return;
    }
    [[HPUIEngine getInstance] showScreen:SCR_INPUT_PWD];
}

- (void)inputCancel
{
    [self.workingProcess dealResult:[HPSwiperResult failResultWithCode:HPSwiperResultCancel]];
}

- (void)inputOver:(NSString *)pwd
{
    [HPUIENG showWaiting:@"请稍候..."];
    [self.workingProcess pinInputed:pwd];
}

- (void)notifyReSwipe:(id)process result:(HPSwiperResult *)result
{
    if (self.selectView)
        [self.selectView removeFromSuperview];
    AlertCommandData* commandCancel = [[AlertCommandData alloc] initCommand:@"取消" function:@selector(swiperCancel)];
    AlertCommandData* commandRedo = [[AlertCommandData alloc] initCommand:@"重刷" function:@selector(reSwipe)];
    
    NSString * msg = @"刷（插）卡失败，请重新刷（插）卡，若读取IC卡，交易完成前请勿拔出卡槽。";
    if (result.code == HPSwiperResultDownGrage)
        msg = TipsICCantSwipe;

    AlertData* alertData = [[AlertData alloc] initAlertData:self title:@"提示" message:msg commands:commandRedo,commandCancel, nil];
    [HPUIENG showAlertWithData:alertData];
    
    [commandCancel release];
    [commandRedo release];
    [alertData release];
}

- (void)processSelectApp:(NSArray *)array
{
    if (array == nil || array.count == 0)
    {
        [HPUIENG showAlert:@"此IC卡不支持该业务，请更换其它银行卡再尝试！"];
        [self.workingProcess.currentSwiper cancelSwiper];
        [HPUIENG popScreen];
    }
    else if(array.count == 1)
    {
        NSArray* type = [array objectAtIndex:0];
        [[HPSwiperContext sharedInstance].avaliableService doSelectApp:type complete:nil];
    }
    else
    {
        self.selectView = [[[HPSelectView alloc] initWithData:array frame:CGRectMake(20, 30, 280, 100) delegate:self] autorelease];
    }
}

+ (void)processFailed:(POSProcess *)process result:(HPSwiperResult *)result
{
    if (result.code == HPSwiperResultNotConnected)
        return;

    if (result.isCanceld || process.currentAction == SwipeActionInputPin || process.currentAction == SwipeActionEncrypt)
        [HPUIENG popScreen];

    if (result.code == HPSwiperResultLowBattery)
    {
        [HPUIENG showAlert:TipsLowBattery];
    }
    else
    {
        if (result.isCanceld)
            [HPUIENG showAlert:TipsCancel];
        else if (process.currentAction == SwipeActionInputPin)
            [HPUIENG showAlert:TipsInputPwdErr];
        else if (process.currentAction == SwipeActionEncrypt)
            [HPUIENG showAlert:TipsEncryptErr];
    }
}

- (void)swiperCancel
{
    [HPUIENG popScreen];
    [self.workingProcess clear];
}

- (void)reSwipe
{
    [self.workingProcess reSwipe];
}

- (void)selectResult:(NSArray *)cardType
{
    [[HPSwiperContext sharedInstance].avaliableService doSelectApp:cardType complete:nil];
}
@end
